﻿/*******************************************************************************
 * Copyright @ 2017 YunSpace.Framework 版权所有
 * Author: GaoTang
 * Description: YunSpace 快速开发平台
 * Website：http://www.yunspace.com.cn
*********************************************************************************/
#region USING SYSTEM
using YunSpace.Core;
using YunSpace.Domain.Entity.OrderFlowManage;
using YunSpace.Domain.IRepository.OrderFlowManage;
using YunSpace.Repository.OrderFlowManage;
using System.Collections.Generic;
using System.Linq;
using YunSpace.Data;
using MySql.Data.MySqlClient;
using YunSpace.Domain.ViewModel.OrderFlowManage;
using YunSpace.Domain.ViewModel;
#endregion

namespace YunSpace.Application.OrderFlowManage
{
    public class ClientOrdertakelookallotApp
    {
        private IClientOrdertakelookallotRepository service = new ClientOrdertakelookallotRepository();

        //public List<ClientOrdertakelookallotEntity> GetList(Pagination pagination, string queryJson)
        //{
        //    var expression = ExtLinq.True<ClientOrdertakelookallotEntity>();
        //    var queryParam = queryJson.ToJObject();
        //    // if (!queryParam["keyword"].IsEmpty())
        //    // {
        //    //     string keyword = queryParam["keyword"].ToString();
        //    //     expression = expression.And(t => t.F_Account.Contains(keyword));
        //    // }
        //    // if (!queryParam["timeType"].IsEmpty())
        //    // {
        //    //     string timeType = queryParam["timeType"].ToString();
        //    //     DateTime startTime = DateTime.Now.ToString("yyyy-MM-dd").ToDate();
        //    //     DateTime endTime = DateTime.Now.ToString("yyyy-MM-dd").ToDate().AddDays(1);
        //    //     switch (timeType)
        //    //     {
        //    //         case "1":
        //    //             break;
        //    //         case "2":
        //    //             startTime = DateTime.Now.AddDays(-7);
        //    //             break;
        //    //         case "3":
        //    //             startTime = DateTime.Now.AddMonths(-1);
        //    //             break;
        //    //         case "4":
        //    //             startTime = DateTime.Now.AddMonths(-3);
        //    //             break;
        //    //         default:
        //    //             break;
        //    //     }
        //    //     expression = expression.And(t => t.F_Date >= startTime && t.F_Date <= endTime);
        //    // }
        //    return service.FindList(expression, pagination);
        //}

        public List<ClientOrderTakelookAllotDto> GetList(Pagination pagination, string queryJson)
        {
            var sqlStr = $@"-- 获取所有已添加的带看分配信息
                            -- 活动主题，带看时间，城市，场地名称，空间名称，销售人员，带看人员
                            SELECT SCOTA.F_Id, SCPB.F_ActivityTheme, SCOTD.F_Source, SCPB.F_ActivityCityId, SS.title F_SiteName, SP.title F_SpaceName, SU1.F_RealName F_FlowUserName, SU2.F_RealName F_TakelookUserName 
                            FROM Site_Client_OrderTakelookAllot SCOTA
                            LEFT JOIN Site_Client_Order SCO ON SCOTA.F_OrderId = SCO.F_Id
                            LEFT JOIN Site_Client_OrderTakelookDetail SCOTD ON SCOTA.F_OrderId = SCOTD.F_OrderId AND SCOTA.F_SiteId = SCOTD.F_SiteId  AND SCOTA.F_SpaceId = SCOTD.F_SpaceId AND SCOTD.F_SourceType=1
                            LEFT JOIN Site_Clients_ProjectByDemand SCPB ON SCO.F_DemandId = SCPB.F_Id
                            LEFT JOIN SL_Site SS ON SCOTA.F_SiteId = SS.F_Id
                            LEFT JOIN SL_Space SP ON SCOTA.F_SiteId = SP.F_SiteId AND SCOTA.F_SpaceId = SP.F_Id
                            LEFT JOIN Sys_User SU1 ON SCO.F_FlowUserId = SU1.F_Id
                            LEFT JOIN Sys_User SU2 ON SCOTA.F_TakelookUserId = SU2.F_Id";
            using (var db = new RepositoryBase())
            {
                var dbParams = new MySqlParameter[] {
                    //new MySqlParameter("@orderId", orderId)
                };
                var result = db.FindList<ClientOrderTakelookAllotDto>(sqlStr, dbParams);
                return result;
            }
        }

        /// <summary>
        /// 获取激活的活动主题
        /// </summary>
        public List<ClientOrderTakelookAllotFormDto> GetActiveOrder() {
            var sqlStr = @"-- 获取所有处于带看状态的活动
                            SELECT SCOT.F_OrderId,  
		                             SCOT.F_No, 
		                             SCPB.F_ActivityTheme, 
		                             SCOT.F_SiteId, 
		                             SS.title F_SiteName, 
		                             SCOT.F_SpaceId, 
		                             SP.title F_SpaceName 
                            FROM Site_Client_Order SCO
                            INNER JOIN Site_Clients_ProjectByDemand SCPB ON SCO.F_DemandId = SCPB.F_Id
                            INNER JOIN Site_Client_OrderTakelook SCOT ON SCO.F_Id = SCOT.F_OrderId
                            LEFT JOIN SL_Site SS ON SCOT.F_SiteId = SS.F_Id
                            LEFT JOIN SL_Space SP ON SCOT.F_SiteId = SP.F_SiteId AND SCOT.F_SpaceId = SP.F_Id
                            WHERE SCO.F_OrderState = 2 AND SCO.F_OrderStateProcess in(-1, 0);";

            using (var db = new RepositoryBase())
            {
                var dbParams = new MySqlParameter[] {
                };
                return db.FindList<ClientOrderTakelookAllotFormDto>(sqlStr, dbParams);
            }
        }

        public List<SelectListItemViewModel> GetUsersByDeptId(string deptId)
        {
            var sqlStr = $@"SELECT SU.F_Id Value, SU.F_RealName Text FROM Sys_User SU WHERE SU.F_DepartmentId = '{ deptId }';";

            using (var db = new RepositoryBase())
            {
                var dbParams = new MySqlParameter[] {
                };
                return db.FindList<SelectListItemViewModel>(sqlStr, dbParams);
            }
        }


            public ClientOrdertakelookallotEntity GetForm(string keyValue)
        {
            return service.FindEntity(keyValue);
        }
        public void DeleteForm(string keyValue)
        {
            service.Delete(t => t.F_Id == keyValue);
        }
        public bool SubmitForm(ClientOrdertakelookallotEntity entity, string keyValue)
        {
            var result = false;
            if (!string.IsNullOrEmpty(keyValue))
            {
                entity.Modify(keyValue);
                service.Update(entity);
            }
            else
            {
                var first = service.IQueryable(p => p.F_OrderId == entity.F_OrderId && p.F_No == entity.F_No && p.F_SiteId == entity.F_SiteId && p.F_SpaceId == entity.F_SpaceId).FirstOrDefault();
                if (first == null)
                {
                    entity.Create();
                    service.Insert(entity);

                    result = true;
                }
            }

            return result;
        }
    }
}

